package com.hxd.otc.module.my.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.hxd.otc.R;
import com.hxd.otc.base.BaseAdapter;
import com.hxd.otc.common.enums.FloatPlatEnum;
import com.hxd.otc.module.my.bean.AdvertBean;
import com.hxd.otc.utils.ResourceUtil;
import com.hxd.otc.utils.StringUtil;

import java.math.BigDecimal;
import java.math.RoundingMode;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by lzy on 2018/6/11 0011.
 * description ${编辑广告Adapter}
 */
public class AdAdapter extends BaseAdapter<AdAdapter.AdHolder, AdvertBean> {

    private OnRevokeAdvertListener onRevokeAdvertListener;

    public AdAdapter(Context context) {
        super(context);
    }

    @Override
    protected void onBindHolder(@NonNull AdHolder holder, final int position) {
        AdvertBean item = getItem(position);
        holder.tvSinglePrice.setText(StringUtil.handleZero(item.getPrice()));
        holder.tvSinglePriceUnit.setText(item.getSupportCurrencyCode());
        holder.tvVolume.setText(String.format(ResourceUtil.getString(R.string.format_advert_nums), String.valueOf(StringUtil.formatNumberRetainZero(
                (Double.valueOf(item.getVolume())
                        - Double.valueOf(item.getLockVolume())
                        - Double.valueOf(item.getSuccessVolume())),
                6, false)), item.getSymbol()));
        boolean isRevoke = (item.getStatus() == 9);//0发布状态 1已完成 9取消
        boolean isComplete = item.getStatus() == 1;
        holder.tvRevoke.setVisibility(isRevoke || isComplete ? View.GONE : View.VISIBLE);
        holder.ivRevoked.setVisibility(isRevoke || isComplete ? View.VISIBLE : View.GONE);
        holder.tvRevoke.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onRevokeAdvertListener != null) {
                    onRevokeAdvertListener.onRevoke(position);
                }
            }
        });
        holder.tvSinglePrice.setTextColor(ResourceUtil.getColor(isRevoke || isComplete ? R.color.color_FF95A2AC : R.color.color_FF323232));
        holder.tvSinglePriceUnit.setTextColor(ResourceUtil.getColor(isRevoke || isComplete ? R.color.color_FF95A2AC : R.color.color_FF141414));
        holder.viewLine.setVisibility(isRevoke || isComplete ? View.GONE : View.VISIBLE);
        holder.tvLowPrice.setVisibility(item.isFloatPrice() ? View.VISIBLE : View.GONE);
        holder.tvLowPrice.setText(String.format(ResourceUtil.getString(R.string.advert_item_Its_price), StringUtil.handleZero(item.getMinPrice()), item.getSupportCurrencyCode()));
        holder.tvQuota.setText(ResourceUtil.getString(R.string.my_advert_quota, StringUtil.handleZero(item.getMinMoney()), StringUtil.handleZero(item.getMaxMoney()), item.getSupportCurrencyCode()));

        String supportPay = item.getSupportPay();
        if (isRevoke || isComplete || TextUtils.isEmpty(supportPay)) {
            holder.ivAlipay.setVisibility(View.GONE);
            holder.ivWeixin.setVisibility(View.GONE);
            holder.ivBankCard.setVisibility(View.GONE);
        } else {//1微信2支付宝3银行卡
            holder.ivAlipay.setVisibility(supportPay.contains("2") ? View.VISIBLE : View.GONE);
            holder.ivWeixin.setVisibility(supportPay.contains("1") ? View.VISIBLE : View.GONE);
            holder.ivBankCard.setVisibility(supportPay.contains("3") ? View.VISIBLE : View.GONE);
        }

        if (isRevoke) {
            holder.ivRevoked.setImageResource(R.mipmap.icon_revoked);
            holder.tvFloatPlat.setVisibility(View.GONE);
            holder.tvFloatRate.setVisibility(View.GONE);
        } else if (isComplete) {
            holder.tvFloatPlat.setVisibility(View.GONE);
            holder.tvFloatRate.setVisibility(View.GONE);
            holder.ivRevoked.setImageResource(R.mipmap.icon_compeleted);
        } else {
            if (item.isFloatPrice()) {
                FloatPlatEnum floatPlatEnum = FloatPlatEnum.getInstance(item.getFloatPlat());
                holder.tvFloatPlat.setText(floatPlatEnum == null ? "" : floatPlatEnum.getValue());
                String floatRate = item.getFloatRate();
                BigDecimal floatRateDecimal = new BigDecimal(floatRate).multiply(new BigDecimal(100)).setScale(8, RoundingMode.DOWN);
                holder.tvFloatRate.setText(String.format("%s%s%s", ResourceUtil.getString(item.isFloatingUpward() ? R.string.floating_upward : R.string.floating_downward),
                        StringUtil.handleZero(floatRateDecimal.toPlainString() + ""), "%"));
                holder.tvFloatRate.setSelected(item.isFloatingUpward());
                holder.tvFloatPlat.setVisibility(View.VISIBLE);
                holder.tvFloatRate.setVisibility(View.VISIBLE);
            } else {
                holder.tvFloatPlat.setVisibility(View.GONE);
                holder.tvFloatRate.setVisibility(View.GONE);
            }
        }
    }


    @NonNull
    @Override
    public AdHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new AdHolder(mInflater.inflate(R.layout.item_advert, parent, false));
    }

    static class AdHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_single_price)
        TextView tvSinglePrice;
        @BindView(R.id.tv_single_price_unit)
        TextView tvSinglePriceUnit;
        @BindView(R.id.iv_alipay)
        ImageView ivAlipay;
        @BindView(R.id.iv_weixin)
        ImageView ivWeixin;
        @BindView(R.id.iv_bank_card)
        ImageView ivBankCard;
        @BindView(R.id.tv_volume)
        TextView tvVolume;
        @BindView(R.id.tv_revoke)
        TextView tvRevoke;
        @BindView(R.id.iv_revoked)
        ImageView ivRevoked;
        @BindView(R.id.tv_quota)
        TextView tvQuota;
        @BindView(R.id.tv_float_plat)
        TextView tvFloatPlat;
        @BindView(R.id.tv_float_rate)
        TextView tvFloatRate;
        @BindView(R.id.tv_low_price)
        TextView tvLowPrice;
        @BindView(R.id.view_line)
        View viewLine;

        AdHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public void setOnRevokeAdvertListener(OnRevokeAdvertListener onRevokeAdvertListener) {
        this.onRevokeAdvertListener = onRevokeAdvertListener;
    }

    public interface OnRevokeAdvertListener {
        void onRevoke(int position);
    }

}
